package Page.BookBorrowingRecordPage;

import Dao.BookBorrowingRecordDao;
import Dao.BookDao;
import DataClass.BookBorrowingRequestRecord;
import Util.TimeUtil;
import javafx.beans.property.*;

/**
 * @since 2024-06-02
 * @author yooo_fan
 */
public class AddRecordViewModel {
    // 数据记录
    private final StringProperty inputPeopleName = new SimpleStringProperty();
    private final StringProperty inputBookName = new SimpleStringProperty();
    private final StringProperty inputBookId = new SimpleStringProperty();
    // 标记是否允许登记
    private final BooleanProperty ifOkStatus = new SimpleBooleanProperty(true);
    private final BooleanProperty insertState = new SimpleBooleanProperty(false);

    public void updateInputPeopleName(String inputPeopleName) {
        this.inputPeopleName.set(inputPeopleName);
    }
    public void updateInputBookName(String inputBookName) {
        this.inputBookName.set(inputBookName);
    }
    public void updateInputBookId(String inputBookId) {
        this.inputBookId.set(inputBookId);
    }

    // 用户点击确定按钮,检查是否存在记录
    public void clickCheckButton(){
        ifOkStatus.set(!BookDao.ifFindBook(inputBookName.get(), inputBookId.get()));
        System.out.println("result: " + ifOkStatus.get());
    }

    /**
     * 插入数据并且更新插入状态
     */
    public void clickConfirmButton(){
        ifOkStatus.set(false);
        try{
            insertState.set(
                    BookBorrowingRecordDao.addBookBorrowingRequestRecord(
                            new BookBorrowingRequestRecord(0,
                                    inputBookId.get(),
                                    "",
                                    TimeUtil.getCurrentTimestamp(),
                                    inputPeopleName.get(),
                                    inputBookName.get()
                            )
                    )
            );
        }catch(Exception e){
            System.err.println("err when click confirm button");
        }
    }

    public StringProperty getInputBookName() {
        return inputBookName;
    }

    public StringProperty getInputBookId() {
        return inputBookId;
    }

    public StringProperty getInputPeopleName() {
        return inputPeopleName;
    }

    public Property<Boolean> getIfOkStatus() {
        return ifOkStatus;
    }
}
